package test.wx;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class AccessToken {
	
	public static final String APPID = "wxa627c0eb81301482";	
	private static final String SECRET = "f36a961e962c09194ef1d0f68cfeec50";
	
	public String testGetAccessToken(){
		String result = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
        	StringBuilder urlSb = new StringBuilder();
        	urlSb.append("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=");
        	urlSb.append(APPID);
        	urlSb.append("&secret=");
        	urlSb.append(SECRET);
            
            HttpGet httpGet = new HttpGet(urlSb.toString());
            CloseableHttpResponse response = httpclient.execute(httpGet);
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            String resultJsonStr = EntityUtils.toString(entity);
            EntityUtils.consume(entity);
            result = resultJsonStr;
        }catch(Exception e){
        	e.printStackTrace();
        } finally {
            try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        return result;
	}
	
	public static void main(String[] args){
		AccessToken at = new AccessToken();
		System.out.println(at.testGetAccessToken());
	}
}
